package com.sgb.kjwl.view.ui.activity.house;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarBrandBinding;

import com.sgb.kjwl.di.machine.DaggerMachineComponent;
import com.sgb.kjwl.di.machine.MachineModule;
import com.sgb.kjwl.model.entity.CarBrandEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseCarBrandEvent;
import com.sgb.kjwl.view.ui.adapter.lease.CarSideBarAdapter;
import com.sgb.kjwl.viewmodel.self.publish.CarBrandModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.SideBar;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick2;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.PinyinUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

/**
 * 车辆品牌
 */
public class CarBrandActivity extends AppActivity {

    @Inject
    CarBrandModel viewModel;
    private ActivityCarBrandBinding binding;
    private List<CarBrandEntity> list = new ArrayList<>();
    private CarSideBarAdapter adapter;
    private LinearLayoutManager linearLayoutManager;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, CarBrandActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMachineComponent.builder().appComponent(appComponent).machineModule(new MachineModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_car_brand);
        binding.include.titleView.setText("品牌");
        binding.include.ivBack.setOnClickListener(v -> finish());

        adapter = new CarSideBarAdapter(this, list);
        linearLayoutManager = new LinearLayoutManager(this);
        binding.rvCar.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0, getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.rvCar.setLayoutManager(linearLayoutManager);
        binding.rvCar.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick2<CarBrandEntity>() {
            @Override
            public void onItemClick(CarBrandEntity carBrandEntity, int position) {
                EventBus.getDefault().post(LeaseCarBrandEvent.make().setMessageTag(EventConstant.EVENT_CAR_BRAND_SELECT).setMessageData(carBrandEntity));
                finish();
            }
        });
        //设sideBar置控件属性
        binding.sideBar.setTextSize(getResources().getDimensionPixelSize(R.dimen.sp11));
        binding.sideBar.setTextColor(Color.parseColor("#5792FD"));
        binding.sideBar.setSelectTextColor(Color.parseColor("#FFFFFF"));
        binding.sideBar.setSelectBackgroundColor(Color.parseColor("#5792FD"));
        binding.sideBar.setDefaultChoose(0);
        binding.sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String letter) {
                //获取该字母首次出现的位置
                int position = adapter.getPositionForSection(letter);
                if (position != -1) {
                    linearLayoutManager.scrollToPositionWithOffset(position, 0);
                }
            }
        });
    }


    @Override
    protected void init() {
        viewModel.getPickerCarBrand();
    }

    public void showBrandList(List<CarBrandEntity> list) {
        this.list = list;
        for (CarBrandEntity entity : list) {
            if ("其他".equals(entity.getName())) {
                entity.setPinyin("#");
                entity.setFirstLetter("#");
                continue;
            }
            entity.setPinyin(PinyinUtils.getPingYin(entity.getName())); // 根据姓名获取拼音
            entity.setFirstLetter(entity.getPinyin().substring(0, 1).toUpperCase()); // 获取拼音首字母并转成大写
            if (!entity.getFirstLetter().matches("[A-Z]")) { // 如果不在A-Z中则默认为“#”
                entity.setFirstLetter("#");
            }
        }

        Collections.sort(list);
        adapter.setDatas(list);
    }
}
